
var oprtCatName = "";
$(document).ready(function(){
    oprtCatName = $("#oprtCatName").val();
    if (oprtCatName != "") {
        cg.oprtCatPno = "";
    }
    cg.loadData();
    
});

var cg = {
    mark: "&nbsp;",
    oprtCatPno: "0",
    writeBack: function(data){
        if (data != "") {
        	//alert(data);
            var obj = $.parseJSON(data);
            if (obj.length > 0) {
                var pId = cg.oprtCatPno;
                // alert(pId+"==pId===");
                // 加载数据之前先清空
				//<tr id="" parentId="" hasLeaf="" kong="" class="l-grid-row" style="display:none">
                // 先清空
                if (pId != "0") {
                    $("#catTable tr[catPno=" + pId + "]").remove();
                }
                for (var i in obj) {
                    var str = '<div class="l-grid-row-cell-inner  cell3">';
                    str += '<a href="javascript:addSubRow(\'' + obj[i].oprtCatNo +'\')">增加子分类</a>&nbsp;';
                    str += '<a href="javascript:editRow(\'' + obj[i].oprtCatNo +'\')">修改</a>&nbsp;';
                    str += '<a href="javascript:deleteRow(\'' + obj[i].oprtCatNo+'\')">删除</a>';
                    var testStr = '<a href="javaScript:getChilds(\'' +obj[i].oprtCatNo +'\')" class="icClose">'
					testStr +='<img src="../../res/images/system/grid-detail-close.gif" width="11" height="11"/>'+
					'<span class="cname">' +obj[i].oprtCatName +'</span></a>&nbsp;';
                    if (pId != "0") {
                        var kong = $("#catTable tr[id=" + pId + "]").attr("kong");
						//alert(kong+"==");
                        testStr = kong + testStr;
						//alert(testStr+"====");
                    }
                    
                    var tr = $("#catTable tr").eq(0).clone();
                    tr.find("td").eq(0).html('<div style="margin-left: 20px;text-align: left;" class="l-grid-row-cell-inner cell1">' +
                    testStr +
                    '</div>');
					tr.find("td").eq(1).html('<div class="l-grid-row-cell-inner cell2">' +
                    '广州百货' +
                    '</div>');
                    tr.find("td").eq(2).html('<div class="l-grid-row-cell-inner cell2">' +
                    obj[i].updateTime+
                    '</div>');
                    tr.find("td").eq(3).html(str);
                    tr.attr("oprtCatPno", '' + obj[i].oprtCatPno + '');
                    tr.attr("id", '' + obj[i].oprtCatNo + '');
                    tr.attr("kong", "&nbsp;&nbsp;&nbsp;");// 用于分级
                    tr.show();
                    
                    if (pId != "0") {
                        var kong = $("#catTable tr[id=" + pId + "]").attr("kong");
                        tr.attr("kong", kong + "&nbsp;&nbsp;&nbsp;");// 用于分级
                        $("#catTable tr[id=" + pId + "]").after(tr);
                    }
                    else {
                        tr.appendTo("#catTable");// 首次加载
                    }
                    
                    // tr鼠标移入事件
                    $(tr).mouseover(function(){
                        if (!$(this).hasClass('l-selected')) {
                            $(this).css("background-color", "#D9E8FB");
                        }
                    });
                    
                    // tr鼠标移出事件
                    $(tr).mouseout(function(){
                        if (!$(this).hasClass('l-selected')) {
                            $(this).css("background-color", "");
                        }
                    });               
                }
            }
        }
    },
    loadData: function(){
    	//
    	var url =  'http://localhost:8080/ECW-MallMgt/commodity/commodity_oper/ajaxcommodity_oper_list.ihtml';
        pageFun.ajaxRequest(url, {
            'oprtCatPno': cg.oprtCatPno,
            'oprtCatName': oprtCatName
        }, cg.writeBack);
    }
    
};

function getChilds(oprtCatPno){
    // 判断是折叠还是展开
    var n = $("#catTable tr[oprtCatPno='" + oprtCatPno + "']").length;
    var obj = $("#catTable tr[id='" + oprtCatPno + "']").find("a:eq(0)");
    var cls = $(obj).attr("class");
    
    if (cls == "icOpen") {// 折叠
   
        delItem(oprtCatPno);
        $(obj).attr("class", "icClose");
        $(obj).find('img').attr('src', '../../res/images/system/grid-detail-close.gif');
    }
    else {
    	//alert("打----开"+"===");
        $(obj).attr("class", "icOpen");
        $(obj).find('img').attr('src', '../../res/images/system/grid-detail-open.gif');
        cg.oprtCatPno = oprtCatPno;
        oprtCatName = "";
        cg.loadData();
    };
    }

 //折叠删除
function delItem(oprtCatPno){
	//alert("delItem===="+oprtCatPno);
    var n = $("#catTable tr[oprtCatPno='" + oprtCatPno + "']").length;
    //alert(n+"=========n");
    if (n > 0) {// 折叠删除
        $("#catTable tr[oprtCatPno='" + oprtCatPno + "']").each(function(){
             delItem($(this).attr("id"));
            $(this).remove();
        });
    }
}

function getTrs(new_tr){
    var pid = $(new_tr).attr('id');
    var otrs = $(new_tr).nextUntil().filter(function(){
        if ($(this).attr('oprtCatPno') == pid) {
            return $(this);
        }
    });
    return otrs;
};

// 得到子项（递归）
function getTrInfo(new_tr, arry, i){
    arry[i] = new_tr;
    i++;
    var otrs = getTrs(new_tr);
    if (otrs != null && otrs.length > 0) {
        for (var j = 0; j < otrs.length; j++) {
            arry[i] = otrs[j];
            i++;
            getTrInfo(otrs[j], arry, i);
        };
            };
    return arry;
}

//==============对连接进行转发===============

function addSubRow(oprtCatNo)
{
	var url =  'http://localhost:8080/ECW-MallMgt/commodity/commodity_oper/commodity_oper_to_def_add.ihtml?oprtCatNo='+oprtCatNo;
	window.location.href=url;
}

function editRow(oprtCatNo)
{   //alert()
	var url =  'http://localhost:8080/ECW-MallMgt/commodity/commodity_oper/commodity_oper_toedit.ihtml?oprtCatNo='+oprtCatNo;
	window.location.href=url;
}

function deleteRow(oprtCatNo)
{
	var url =  'http://localhost:8080/ECW-MallMgt/commodity/commodity_oper/commodity_oper_del.ihtml?oprtCatNo='+oprtCatNo;
	window.location.href=url;
}
